// Coded by Shambler (Shambler@OldUnreal.com, Shambler__@Hotmail.com, ICQ: 108730864) and portal effects by Fuegerstef
Class pgPortalProjectile extends ShotgunBullet;//LinkProjectile;

var pgPortalGun WeaponOwner;

var byte FireMode;

// Distance to put portal out from wall (affects the impact emitter too)
var float PortalDistance;

var pgProjEmitter ProjEffect;

replication
{
	reliable if( Role == ROLE_Authority )
		FireMode;
}

simulated function PostBeginPlay()
{
	Velocity = vector(Rotation) * Speed;

	Super.PostBeginPlay();
}

simulated function PostNetBeginPlay()
{
	if( Level.Netmode != NM_DedicatedServer )
	{
		ProjEffect = Spawn(Class'pgProjEmitter', self);
		ProjEffect.SetColour(FireMode);
	}
	Acceleration = Normal(Velocity) * 3000.0;
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
	local pgProjImpactEmitter IE;

	if (EffectIsRelevant(Location, False))
	{
		IE = Spawn(Class'pgProjImpactEmitter',,,HitLocation + (HitNormal * (PortalDistance + 10.0)));
		IE.SetColour(FireMode);
	}

	Destroy();
}

simulated function Destroyed()
{
	if (ProjEffect != none)
		ProjEffect.Destroy();

	Super.Destroyed();
}

final function PlayInvalidSnd()
{
	PlaySound(Sound'portal_invalid_surface',SLOT_Pain,2.f);
}
simulated function HitWall(vector HitNormal, Actor Wall)
{
	local pgPortalDecal pd;
	local pgNoPortalSurface V;
	local vector TestPos;
	local rotator TestRot;
	local bool bFoundPortalSurf,bFoundPortalNoSurf;

	if( Level.Netmode!=NM_Client && WeaponOwner!=None )
	{
		foreach TouchingActors(Class'pgNoPortalSurface',V)
		{
			bFoundPortalNoSurf = true;
			bFoundPortalSurf = (bFoundPortalSurf || pgPortalSurface(V)!=None);
		}
		if( !bFoundPortalNoSurf || bFoundPortalSurf )
		{
			TestPos = Location + (HitNormal * PortalDistance);
			TestRot = GetRotOrientation(HitNormal);
			if( Class'pgPortalDecal'.Static.TryToFit(TestPos,TestRot,WeaponOwner,FireMode) )
			{
				pd = Spawn(Class'pgPortalDecal',InstigatorController,,TestPos,TestRot);
				pd.WeaponOwner = WeaponOwner;
				pd.FireMode = FireMode;
				if( Wall!=None && Wall!=Level && !Wall.bStatic )
					pd.SetBase(Wall);
				pd.OpenPortal();
			}
			else PlayInvalidSnd();
		}
		else PlayInvalidSnd();
	}
	Super.HitWall(HitNormal, Wall);
}
final function rotator GetRotOrientation( vector Wall )
{
	local vector X;
	local rotator R;

	if( Abs(Wall.Z)<0.8f )
		return Rotator(Wall);
	R.Yaw = Rotation.Yaw;
	X = vector(R);
	X = Normal(X - (X dot Wall) * Wall);
	return OrthoRotation(Wall,Normal(X Cross Wall),X);
}

defaultproperties
{
     PortalDistance=8.000000
     Speed=4000.000000
     MaxEffectDistance=7000.000000
     LightType=LT_Steady
     LightEffect=LE_QuadraticNonIncidence
     LightHue=100
     LightSaturation=100
     LightBrightness=255.000000
     LightRadius=3.000000
     CullDistance=3500.000000
     bDynamicLight=True
     LifeSpan=1.000000
     AmbientGlow=217
     FluidSurfaceShootStrengthMod=6.000000
     SoundVolume=255
     SoundRadius=50.000000
	TransientSoundRadius=400
}
